﻿/* ***********************************************
 * Author		:  kingthy
 * Email		:  kingthy@gmail.com
 * Description	:  OAuth授权对象
 *
 * ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using OpenTwitter.SDK.Http;
using System.Web;
using OpenTwitter.SDK.Tencent.Objects;

namespace OpenTwitter.SDK.Tencent
{
    /// <summary>
    /// OAuth授权对象
    /// </summary>
    public class OAuth : OAuthBase
    {
        /// <summary>
        /// 根据app_key与app_secret实例化
        /// </summary>
        public OAuth(string appKey, string appSecret)
            : base(appKey, appSecret)
        { }
        /// <summary>
        /// 使用默认的API地址同步获取未授权的Request Token. 
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
        /// </summary>
        /// <param name="callback">返回地址</param>
        /// <returns>是否获取request token成功</returns>
        public bool GetRequestToken(string callback)
        {
            return this.GetRequestToken("https://open.t.qq.com/cgi-bin/request_token", callback);
        }

        /// <summary>
        /// 同步获取未授权的Request Token. 
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为request_token与request_secret
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="callback">返回地址</param>
        /// <returns>是否获取request token成功</returns>
        public override bool GetRequestToken(string requestUrl, string callback)
        {
            this.Token = string.Empty;
            this.TokenSecret = string.Empty;
            this.Clear();
            NameValueCollection responseData;
            //腾讯的API里,当callback为空时需要传送null值.所以此函数需要重写
            this.Parameters.Add("oauth_callback", string.IsNullOrEmpty(callback) ? "null" : callback);
            var r = this.GetOAuthToken(requestUrl, out responseData);

            return r;
        }
        /// <summary>
        /// 使用默认的API地址同步获取授权的Access Token，调用此方法时必须设置Token与TokenSecret属性的值为request_token与request_secret
        /// 如果获取成功, 对象实例的Token与TokenSecret属性值将改为access_token与access_secret
        /// </summary>
        /// <param name="verifier">请求授权request token时返回的验证码</param>
        /// <param name="user">微博帐户名</param>
        /// <returns>是否获取Access Token成功</returns>
        public bool GetAccessToken(string verifier, out AuthorizedUser user)
        {
            NameValueCollection responseData;
            var r = this.GetAccessToken("https://open.t.qq.com/cgi-bin/access_token", verifier, out responseData);
            string name = r && responseData != null ? responseData["name"] : null;

            if (string.IsNullOrEmpty(name))
            {
                user = null;
            }
            else
            {
                user = new AuthorizedUser(string.Empty, name);
            }
            return r;
        }
    }
}
